home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg2 / vulprog2.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-08  |  1.1 KB  |  49 lines

  1. /* Just the vulnerable program we will exploit.    */
  2. /* To compile use: gcc -o exploit1 exploit1.c -ldl */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <dlfcn.h>
  9.  
  10. #define ERROR -1
  11. #define BUFSIZE 16
  12.  
  13. int goodfunc(const char *str); /* funcptr starts out as this */
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.    static char buf[BUFSIZE];
  18.    static int (*funcptr)(const char *str);
  19.  
  20.    if (argc <= 2)
  21.    {
  22.       fprintf(stderr, "Usage: %s <buffer> <goodfunc's arg>\n", argv[0]);
  23.       exit(ERROR);
  24.    }
  25.  
  26.    printf("system()'s address = %p\n", &system);
  27.  
  28.    funcptr = (int (*)(const char *str))goodfunc;
  29.    printf("before overflow: funcptr points to %p\n", funcptr);
  30.  
  31.    memset(buf, 0, sizeof(buf));
  32.    strncpy(buf, argv[1], strlen(argv[1]));
  33.    printf("after overflow: funcptr points to %p\n", funcptr);
  34.  
  35.    (void)(*funcptr)(argv[2]);
  36.    return 0;
  37. }
  38.  
  39. /* ---------------------------------------------- */
  40.  
  41. /* This is what funcptr should/would point to if we didn't overflow it */
  42. int goodfunc(const char *str)
  43. {
  44.    printf("\nHi, I'm a good function. I was called through funcptr.\n");
  45.    printf("I was passed: %s\n", str);
  46.  
  47.    return 0;
  48. }
  49.